package cn.iocoder.yudao.module.system.controller.admin.userpointslog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.userpointslog.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userpointslog.UserPointsLogDO;
import cn.iocoder.yudao.module.system.service.userpointslog.UserPointsLogService;

@Tag(name = "管理后台 - 用户积分操作日志")
@RestController
@RequestMapping("/system/user-points-log")
@Validated
public class UserPointsLogController {

    @Resource
    private UserPointsLogService userPointsLogService;

    @PostMapping("/create")
    @Operation(summary = "创建用户积分操作日志")
    @PreAuthorize("@ss.hasPermission('system:user-points-log:create')")
    public CommonResult<Long> createUserPointsLog(@Valid @RequestBody UserPointsLogSaveReqVO createReqVO) {
        return success(userPointsLogService.createUserPointsLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户积分操作日志")
    @PreAuthorize("@ss.hasPermission('system:user-points-log:update')")
    public CommonResult<Boolean> updateUserPointsLog(@Valid @RequestBody UserPointsLogSaveReqVO updateReqVO) {
        userPointsLogService.updateUserPointsLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户积分操作日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:user-points-log:delete')")
    public CommonResult<Boolean> deleteUserPointsLog(@RequestParam("id") Long id) {
        userPointsLogService.deleteUserPointsLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户积分操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:user-points-log:query')")
    public CommonResult<UserPointsLogRespVO> getUserPointsLog(@RequestParam("id") Long id) {
        UserPointsLogDO userPointsLog = userPointsLogService.getUserPointsLog(id);
        return success(BeanUtils.toBean(userPointsLog, UserPointsLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户积分操作日志分页")
    @PreAuthorize("@ss.hasPermission('system:user-points-log:query')")
    public CommonResult<PageResult<UserPointsLogRespVO>> getUserPointsLogPage(@Valid UserPointsLogPageReqVO pageReqVO) {
        PageResult<UserPointsLogDO> pageResult = userPointsLogService.getUserPointsLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPointsLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户积分操作日志 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-points-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPointsLogExcel(@Valid UserPointsLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPointsLogDO> list = userPointsLogService.getUserPointsLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户积分操作日志.xls", "数据", UserPointsLogRespVO.class,
                        BeanUtils.toBean(list, UserPointsLogRespVO.class));
    }

}